<template>
  <PageLayout>
    <!-- <template #header>
      <BasicForm :options="queryOptions" @ok="onQueryDataByParams"></BasicForm>
    </template> -->
    <div class="table-box">
      <div class="head-box">
        <div class="title">APP版本更新</div>
        <a-space :size="16">
          <a-button type="primary" @click="edit()">新建</a-button>
        </a-space>
      </div>
      <div class="bg-fff">
        <!-- :apiFetch="postAppVersionPage"  -->
        <BasicTable
          ref="tableRef"
          :columns="columns"
          :query-params="queryData"
          :apiFetch="postAppVersionPage"
        >
          <template #bodyCell="{ column, record }">
            <template v-if="column.key === 'avatar'">
              <a-image :height="50" :width="50" :src="record.avatar" />
            </template>
            <template v-if="column.key === 'action'">
              <a-space class="space-btn--box" :size="10">
                <a class="btn" @click="edit(record)">下载</a>
                <a class="btn" @click="remove(record)">删除</a>
              </a-space>
            </template>
          </template>
        </BasicTable>
      </div>
    </div>
  </PageLayout>
</template>

<script name="app-list" setup>
import {
  postAppVersionPage,
  postAppVersionDelete,
  postClientUserExport,
  postClientUserUpdateStatus,
} from "@/api/common";
import { useStoreCommonEnum } from "@/store";
import { comfirmModal } from "@/hooks/useModal";
import { message } from "ant-design-vue";
const storeCommonEnum = useStoreCommonEnum();
const router = useRouter();
const dataSource = ref(
  Array.from({ length: 11 }, () => ({
    username: "2121",
  }))
);
// const queryOptions = ref([
//   {
//     name: "姓名",
//     type: "input",
//     value: "",
//     placeholder: "请输入",
//     filed: "keyWords",
//     labelCol: { width: "120px" },
//   },
//   {
//     name: "邮箱",
//     type: "input",
//     value: "",
//     placeholder: "请输入",
//     filed: "keyWords",
//     labelCol: { width: "120px" },
//   },
//   {
//     name: "处理状态",
//     type: "select",
//     value: undefined,
//     placeholder: "请选择",
//     filed: "status",
//     list: [
//       {
//         label: "未处理",
//         value: 0,
//       },
//       {
//         label: "处理中",
//         value: 1,
//       },
//       {
//         label: "已处理",
//         value: 2,
//       },
//     ],
//     attrs: { allowClear: true },
//   },
//   {
//     name: "反馈时间",
//     type: "range-picker",
//     value: [],
//     placeholder: "请选择",
//     filed: "rangeDate",
//     dateFields: ["startCreateTime", "endCreateTime"],
//   },
// ]);
const columns = [
  {
    title: "版本名称",
    dataIndex: "name",
    key: "name",
    width: "150px",
    ellipsis: true,
  },
  {
    title: "版本号",
    dataIndex: "version",
    key: "version",
    width: "200px",
    ellipsis: true,
  },
  {
    title: "系统类型",
    dataIndex: "os",
    key: "os",
    width: "200px",
    ellipsis: true,
    customRender: ({ record }) => `${record.os == 0 ? "iOS" : "Android"}`,
  },
  {
    title: "版本描述",
    dataIndex: "remark",
    key: "remark",
    width: "200px",
    ellipsis: true,
  },
  {
    title: "文件名称",
    dataIndex: "fileName",
    key: "fileName",
    width: "200px",
    ellipsis: true,
  },

  {
    title: "更新时间",
    dataIndex: "updateTime",
    key: "updateTime",
    width: "200px",
    ellipsis: true,
  },

  {
    title: "操作",
    fixed: "right",
    dataIndex: "action",
    key: "action",
    width: 200,
  },
];

const edit = (record) => {
  router.push({
    path: "/app/add",
    query: {
      id: record?.id,
    },
  });
};
const remove = ({ id }) => {
  comfirmModal({
    content: "确认删除app版本",
    onOk() {
      postAppVersionDelete({ id }).then(() => {
        message.success("删除成功");
        tableRef.value.fetch();
      });
    },
  });
};

const queryData = reactive({});
const tableRef = ref(null);
const onQueryDataByParams = (params) => {
  for (let key in params) {
    queryData[key] = params[key];
  }
  tableRef.value.refreshTableData();
};

const toDetail = (record) => {
  router.push({
    path: "/feedback/detail",
    query: {
      id: record?.id,
    },
  });
};

const downFile = ({ fileUrl, fileName }) => {
  var eleLink = document.createElement("a");
  eleLink.download = fileName;
  eleLink.style.display = "none";
  eleLink.target = "_blank";
  eleLink.href = fileUrl;
  eleLink.setAttribute("download", fileName);
  // 触发点击
  document.body.appendChild(eleLink);
  eleLink.click();
  // 然后移除
  document.body.removeChild(eleLink);
};

const changeStatus = (record) => {
  const title = `${record.status ? "启用" : "停用"}`;
  comfirmModal({
    content: `确认${title}该用户账号吗？`,
    onOk() {
      postClientUserUpdateStatus({
        id: record.id,
        status: !record.status,
      }).then(() => {
        message.success(`${title}成功`);
        tableRef.value.fetch();
      });
    },
  });
};
// const remove = (id) => {
//   comfirmModal({
//     content: "确认删除该账号",
//     onOk() {
//       postUserDelete({ id }).then(() => {
//         message.success("删除成功");
//         tableRef.value.fetch();
//       });
//     },
//   });
// };

const importFile = () => {
  if (!tableRef.value.data?.length) {
    message.error("暂无数据");
    return;
  }
  const params = {};

  for (let key in queryData) {
    params[key] = queryData[key];
  }
  postClientUserExport(params).then((res) => {
    message.success("导出成功");
  });
};
</script>

<style lang="less" scoped></style>
